﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataTransferObject;
namespace _5ChickDeal.UserControl
{
    public partial class MainContent : System.Web.UI.UserControl
    {
        Boolean banchuaduoc = true;
        Boolean dabanduoc = false;
        int soPhieu;
        DataClassesDataContext db;
        int id = -1;
        public static string Background="";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["id"] == null)
            {
                return;
            }
            try
            {
                id = int.Parse(Request.QueryString["id"]);

            }
            catch
            {
                return;
            }
            
            db = new DataClassesDataContext();
            var deal = (from d in db.Deals where d.id == id select d).FirstOrDefault();
            int soLuong = deal.DonHangs.Sum(dh => dh.soluong) == null ? 0 : deal.DonHangs.Sum(dh => dh.soluong).Value;
            soPhieu = soLuong;
            if (soLuong >= deal.soluongmuatoithieu)
            {
                banchuaduoc = false;
            }
            else
            {
                banchuaduoc = true;
            }
            Background = deal.HinhAnhs.FirstOrDefault().ten;
            dabanduoc = !banchuaduoc;
            DataFormViewBind();
        }
        private String ThoiGianConLai(DateTime d1, DateTime d2)
        {
            TimeSpan ts = d2 - d1;
            if (ts.Days > 1)
            {
                return ts.Days.ToString() + " ngày <span id='gio'></span>";
            }
            if (ts.Hours < 0)
            {
                return "0:00:00<span id='gio'></span>";
            }
            return "<span id='gio'>" + ts.Hours.ToString() + "</span>:<span id='phut'>" +
                 ts.Minutes + "</span>:<span id='giay'>" + ts.Seconds + "</span>";
        }
        private int SoLuongMua(Deal d)
        {
            int sl = 0;
            try
            {
                sl = d.DonHangs.Sum(dh => dh.soluong).Value;

            }
            catch { }
            return sl;
        }
        private void DataFormViewBind()
        {


            var source = from d in db.Deals
                         where d.id == id

                         select new
                         {
                             id = d.id,
                             ten = d.ten,
                             gia = d.gia,
                             giaconlai = d.giaconlai,
                             giam = 100 - (int)((float)d.giaconlai / d.gia * 100),
                             hinh = d.HinhAnhs.First().ten,
                             thoigianconlai = ThoiGianConLai(DateTime.Now, d.thoigianketthuc.Value),
                             sophieu = soPhieu,
                             thoigiandat = d.thoigiandat != null ? "đạt lúc " + d.thoigiandat.Value.ToString() : null,
                             chitiet = d.chitiet,
                             diemnoibat = d.diemnoibat,
                             dieukiensudung = d.dieukiensudung,
                             hinhdiachi = d.CongTy1.hinhdiachi,
                             soluongtoithieu = d.soluongmuatoithieu,
                             phantram = ((int?)((float)soPhieu / d.soluongmuatoithieu * 100)),
                             canthem = d.soluongmuatoithieu - soPhieu,
                             banchuaduoc = this.banchuaduoc,
                             dabanduoc = !this.banchuaduoc

                         };

            formview1.DataSource = source;
            formview1.DataBind();
        }
    }
}